$(function() {
	var printPage = {
		createNew: function() {
			var print = {};
			var queryreport = QueryReport.createNew();
			print.init = function() {
				this.initData();
				this.bindEvent();
			};
			print.initData = function() {
				var data = getLocalStorage(STORATE_MANAGER.queryresult);
				var queryinfor = getLocalStorage(STORATE_MANAGER.queryinfo);
				print.paintResult(data, queryinfor);
			};

			print.paintResult = function(data, apiinfo) {
				var i = 0;
				var html = [];
				html.push(print.paintQueryItem(data, apiinfo));
				html.push(print.paintQueryItemResult(data));
				html.push(print.paintDisclaimer());
				$("#main").html(html.join(''));
			};
			/*------------------------------查询类目------------------------------*/
			print.paintQueryItem = function(data, queryApiinfor) {
				var html = [];
				var addedParam = [];
				var apinos = data.data.api_nos;
				var params = data.data.params_json;
				var apiinfor = queryApiinfor.data.api_info;
				var title = "个人用户报告页";
				var i = 0;
				var j = 0;
				var k = 0;
				var m = 0;
				if (apinos[0].indexOf("B") > -1) {
					title = "企业用户报告页";
				}
				html.push('<div class="date-box">');
				html.push('<div class="date-tit" style="text-align:center;">');
				html.push(title);
				html.push('<span>' + '' + '</span>')
					//打印报告
				html.push('<button type="button" id="printreport">打印报告</button>');
				html.push('</div>');

				html.push('<div class="date-con">');
				html.push('<p>基本信息</p>');

				html.push('<ul class="personalInfo">');
				for (var checkParam in params) {
					j = 0;
					while (apiinfor[j]) {
						var apilist = apiinfor[j].api_list;
						k = 0;
						while (apilist[k]) {
							var param = apilist[k].params;
							m = 0;
							while (param[m]) {
								if (param[m].key == checkParam && (addedParam.indexOf(checkParam) == -1)) {
									addedParam.push(checkParam);
									html.push('<li id="p_checkparam_' + checkParam + '"><span><label>' + param[m].name + '：</label><b>' + params[checkParam] + '</b></span></li>');
								}
								m++;
							}
							k++;
						}
						j++;
					}
				}
				html.push('</ul>');

				html.push('<p>查询类目 ');
				html.push('<span style="margin-left: 16px;"><input id="showAll" type="checkbox" checked >');
				html.push('<label for="showAll" style="font-weight: 400; color: #666666;font-size:13px;">全选(点击可隐藏类目)</label></span>');
				html.push('</p>');
				html.push('<ul id="personalinfo" class="personalInfo">');
				i = 0;
				var queryArray = getLocalStorage(STORATE_MANAGER.queryParamArray);
				while (apinos[i]) {
					html.push('<li>');
					html.push('<input id="' + apinos[i] + '" type="checkbox" checked>');
					html.push('<label for=' + apinos[i] + '>' + queryArray[apinos[i]] + '</label>');
					html.push('</li>');
					i++;
				}
				html.push('</ul>');
				html.push('</div>');
				html.push('</div>');

				return html.join('');
			};

			/*------------------------------查询接口项------------------------------*/
			print.paintQueryItemResult = function(data) {
				var html = [];
				var ddata = data.data.data;
				var getType = Object.prototype.toString;
				var queryParamArray = getLocalStorage(STORATE_MANAGER.queryParamArray);
				for (var dkey in ddata) {

					var result = ddata[dkey].result;
					var type = getType.call(result);
					switch (type) {
						case '[object String]':
							{
								html.push('<div id="' + ddata[dkey].key + '" name="divcheckitem">');
								html.push('<h2 class="date-h2">' + ddata[dkey].name + '</h2>');
								html.push('<div class="date-box mt30">');
								html.push('<div class="date-tit"><b>' + ddata[dkey].name + '</b></div>');
								html.push('<div class="date-table-box">');
								html.push('<table class="date-table" border="1px" cellspacing="0px">');
								html.push('<tbody><tr>');
								html.push('<td>');
								html.push(((result == '1') ? "认证成功" : "认证失败"));
								html.push('</td>');
								html.push('</tr>');
								html.push('</tbody></table>');
								html.push('</div>');
								html.push('</div>');
								html.push('</div>');
							}
							break;

						case "[object Array]":
							{
								var i = 0;
								if (["judgement"].indexOf(dkey) > -1) {
									var judegCount = result.length;
									html.push('<div id="' + ddata[dkey].key + '" name="divcheckitem">');
									html.push('<h2 class="date-h2">' + ddata[dkey].name + '</h2>');
									html.push('<div class="date-box mt30">');
									html.push('<div class="date-tit"><b>' + ddata[dkey].name + '</b></div>');
									html.push('<div class="date-table-box">');

									if (judegCount > 0) {
										html.push('<table class="date-table" border="1px" cellspacing="0px">');
										html.push('<tbody>');
										html.push('<tr>');
										html.push('<td style="width:50%;"><label class="titlefont">判决信息：</label><label class="redcolorB10319blod">' + (judegCount > 0 ? '有' : '无') + '</label></td>');
										html.push('<td><label class="titlefont">法院判决次数：</label><span>' + judegCount + '次</span></td>');
										html.push('</tr>');
										html.push('</tbody></table>');
										if (judegCount > 0) {
											html.push('<table style="margin-top:15px;" class="date-table" border="1" cellspacing="0px">');
											html.push('<tbody>');
											var columnCount = 2;
											var count = result.length;
											i = 0;
											while (result[i]) {
												if ((i % columnCount) == 0) {
													html.push('<tr><td style="width:50%;"><span>' + result[i] + '</span></td>');
												} else {
													html.push('<td><span>' + result[i] + '</span></td></tr>');
												}
												if (((i + 1) == count) && (i % columnCount) == 0) {
													html.push('<td><span>' + '' + '</span></td></tr>');
												}
												i++;
											}
											html.push('</tbody>');
											html.push('</table>');
										}
									}
									html.push('</div>');
									html.push('</div>');
									html.push('</div>');
								} else if (["merchant_verify"].indexOf(dkey) > -1) {
									var tradeCount = result.length;
									html.push('<div id="' + ddata[dkey].key + '" name="divcheckitem">');
									html.push('<h2 class="date-h2">' + ddata[dkey].name + '</h2>');
									html.push('<div class="date-box mt30">');
									html.push('<div class="date-tit"><b>' + ddata[dkey].name + '</b></div>');
									html.push('<div class="date-table-box">');

									if (tradeCount > 0) {
										html.push('<table class="date-table" border="1" cellspacing="0px">');
										html.push('<tbody>');
										html.push('<tr>');
										html.push('<th>' + thisUtilities.ResultItem[dkey].pName.transTime + '</th>');
										html.push('<th>' + thisUtilities.ResultItem[dkey].pName.transAmount + '</th>');
										html.push('</tr>');
										while (result[i]) {
											html.push('<tr>');
											html.push('<td><span>' + result[i].transTime + '</span></td>');
											html.push('<td style="width:50%;"><span>' + result[i].transAmount + '</span></td>');
											html.push('</tr>');
											i++;
										}
										html.push('</tbody>');
										html.push('</table>');
									}
									html.push('</div>');
									html.push('</div>');
									html.push('</div>');
								} else {
									html.push('<div id="' + ddata[dkey].key + '" name="divcheckitem">');
									html.push('<h2 class="date-h2">' + ddata[dkey].name + '</h2>');
									html.push('<div class="date-box mt30">');
									html.push('<div class="date-tit"><b>' + ddata[dkey].name + '</b></div>');
									html.push('<div class="date-table-box">');
									html.push('<table class="date-table" border="1" cellspacing="0px">');
									html.push('<tbody>');
									while (result[i]) {
										type = getType.call(result[i]);
										html.push(print.paintObject(result[i], dkey));
										i++;
									}
									html.push('</tbody>');
									html.push('</table>');
									html.push('</div>');
									html.push('</div>');
									html.push('</div>');
								}
							}
							break;
						case "[object Object]":
							{
								//对象包含对象
								if (['bank_card_report', 'trans_location_stat', 'merchant_report', 'enterprise_info'].indexOf(dkey) > -1) {
									html.push('<div id="' + ddata[dkey].key + '" name="divcheckitem">');
									html.push('<h2 class="date-h2">' + ddata[dkey].name + '</h2>');
									for (var key in result) {
										html.push('<div class="date-box mt30">');
										html.push('<div class="date-tit"><b>' + thisUtilities.ResultItem[key].name + '</b></div>');
										html.push('<div class="date-table-box">');
										html.push('<table class="date-table" border="1" cellspacing="0px">');
										html.push('<tbody>');
										type = getType.call(result[key]);
										if (type == "[object String]") {
											html.push(print.paintString(result[key], key));
										} else if (type == "[object Array]") {
											html.push(print.paintArray(result[key], key));
										} else if (type == "[object Object]") {
											html.push(print.paintObject(result[key], key));
										}
										html.push('</tbody>');
										html.push('</table>');
										html.push('</div>');
										html.push('</div>');
									}
									html.push('</div>');
								} //对象包含数组，个人职业资格证书
								else if (['professional_certs'].indexOf(dkey) > -1) {
									html.push('<div id="' + ddata[dkey].key + '" name="divcheckitem">');
									html.push('<h2 class="date-h2">' + ddata[dkey].name + '</h2>');
									html.push('<div class="date-box mt30">');
									html.push('<div class="date-tit"><b>' + ddata[dkey].name + '</b></div>');									
									html.push('<div class="date-table-box">');
									html.push('<table class="date-table" border="1" cellspacing="0px">');
									html.push('<tbody>');
									html.push('<tr>');
									html.push('<td style="width:33.3%"><label class="titlefont">' + thisUtilities.ResultItem[dkey].pName.name + '：</label><span>'+ result.name +'</span></td>');
									html.push('<td style="width:33.3%"><label class="titlefont">' + thisUtilities.ResultItem[dkey].pName.certificateID + '：</label><span>'+ result.certificateID +'</span></td>');
									html.push('<td></td>');
									html.push('</tr>');								
									html.push('</tbody>');
									html.push('</table>');
									html.push('</div>');
									
									var list = result.list;
									$.each(list, function(key, item) {
										html.push('<div class="date-table-box">');										
										html.push('<table  class="date-table" border="1" cellspacing="0px">');
										html.push('<tbody>');
										html.push(print.paintObject(item, dkey));
										html.push('</tbody>');
										html.push('</table>');
										html.push('</div>');									
									});
									
									html.push('</div>');
									html.push('</div>');
								}
								//对象包含图片
								else if (['id_photo', 'education'].indexOf(dkey) > -1) {
									html.push('<div id="' + ddata[dkey].key + '" name="divcheckitem">');
									html.push('<h2 class="date-h2">' + ddata[dkey].name + '</h2>');
									html.push('<div class="date-box mt30">');
									html.push('<div class="date-tit"><b>' + ddata[dkey].name + '</b></div>');
									html.push('<div class="date-table-box">');
									html.push('<table class="date-table" border="1" cellspacing="0px">');
									html.push('<tbody>');
									html.push(print.piantPhotoObject(result, dkey));
									html.push('</tbody>');
									html.push('</table>');
									html.push('</div>');
									html.push('</div>');
									html.push('</div>');
								}
								//对象中有布尔类型，如果是否不用画其他的，是就画
								else if (['dishonesty', 'p2p_black'].indexOf(dkey) > -1) {
									html.push('<div id="' + ddata[dkey].key + '" name="divcheckitem">');
									html.push('<h2 class="date-h2">' + ddata[dkey].name + '</h2>');
									html.push('<div class="date-box mt30">');
									html.push('<div class="date-tit"><b>' + ddata[dkey].name + '</b></div>');
									html.push('<div class="date-table-box">');

									if (result.hasOwnProperty('is_have')) {
										//result.is_have = true;
										if (result.is_have) {
											html.push('<table class="date-table" border="1" cellspacing="0px" style="border-bottom: 0;">');
											html.push('<tbody>');
											html.push('<td style="border-bottom: 0;">');
											html.push('<label class="titlefont">');
											html.push(thisUtilities.ResultItem[dkey].pName.is_have);
											html.push('： </label>');
											html.push('<label class="redcolorB10319blod">是</label>');
											html.push('</td>');
											html.push('</tbody>');
											html.push('</table>');
											delete result['is_have'];
											html.push('<table class="date-table" border="1" cellspacing="0px">');
											html.push('<tbody>');
											html.push(print.paintObject(result, dkey, 2));
											html.push('</tbody>');
											html.push('</table>');
										} else {
											html.push('<table class="date-table" border="1" cellspacing="0px">');
											html.push('<tbody>');
											html.push('<td>');
											html.push('<label class="titlefont">');
											html.push(thisUtilities.ResultItem[dkey].pName.is_have);
											html.push('： </label>');
											html.push('<label class="redcolorB10319blod">否</label>');
											html.push('</td>');
											html.push('</tbody>');
											html.push('</table>');
										}
									} else {
										html.push('<table border="1" cellspacing="0px" class="table-box">');
										html.push('<tbody>');
										html.push(print.paintObject(result, dkey));
										html.push('</tbody>');
										html.push('</table>');
									}

									html.push('</div>');
									html.push('</div>');
									html.push('</div>');
								}
								//对象包含元素
								else {
									html.push('<div id="' + ddata[dkey].key + '" name="divcheckitem">');
									html.push('<h2 class="date-h2">' + ddata[dkey].name + '</h2>');
									html.push('<div class="date-box mt30">');
									html.push('<div class="date-tit"><b>' + ddata[dkey].name + '</b></div>');
									html.push('<div class="date-table-box">');
									html.push('<table class="date-table" border="1" cellspacing="0px">');
									html.push('<tbody>');
									html.push(print.paintObject(result, dkey));
									html.push('</tbody>');
									html.push('</table>');
									html.push('</div>');
									html.push('</div>');
									html.push('</div>');
								}
							}
							break;
					}
				}
				return html.join('');
			};
			/*------------------------------字符串类型------------------------------*/
			print.paintString = function(data, key) {
				var html = [];
				html.push('<tr>');
				html.push('<td class="titlefont" style="color:#000000;width:50%;text-align:center;">区间值</td>');
				html.push('<td style="text-align:center;">' + data + '</td>');
				html.push('</tr>');
				return html.join('');
			};
			/*------------------------------对象类型------------------------------*/
			print.paintObject = function(data, propertyKey, column) {
				var html = [];
				var count = 0;
				var multiplueColumn = false;
				if (['indexContrast'].indexOf(propertyKey) > -1) {
					for (var key in data) {
						html.push('<tr>');
						html.push('<td>' + key + '</td>');
						for (var innerkey in data[key]) {
							html.push('<td>' + data[key][innerkey] + '</td>');
						}
						html.push('</tr>');
					}
				} else {
					if (!column) {
						column = 3;
					}
					var size = 0;
					var pName = thisUtilities.ResultItem[propertyKey].pName;
					for (var key in pName) {
						size++;
					}
					size = size - 1;
					for (var key in pName) {
						if ((count % column) == 0) {
							html.push('<tr>');
						}
						var getType = Object.prototype.toString;
						var type = getType.call(data[key]);
						if (["idCardPhoto", "photo"].indexOf(key) > -1) {
							html.push('<td class="titlefont" style="width:33.3%;">');
							html.push(thisUtilities.ResultItem[propertyKey].pName[key] + ":");
							html.push('<img id="idCardPhoto" width="20px" height="16px" src="data:image/png;base64,' + data[key] + '">')
							html.push('</td>');
						} else {
							html.push('<td style="width:33.3%;">');
							html.push('<label class="titlefont">' + thisUtilities.ResultItem[propertyKey].pName[key] + "： </label>");
							html.push(((type == "[object Boolean]") ? ((data[key] == true) ? "<span>是<span/>" : "<span>否</span>") : (['entStatus'].indexOf(key) > -1) ? ('<span>' + data[key] + '</span>') : ('<span>' + data[key] + '</span>')));
							html.push('</td>');
						}
						//最后补充空白td
						if ((size == count) && [0, 1].indexOf(count % column) > -1) {
							var leftCount = column - (size % column) - 1;
							for (var i = 0; i < leftCount; i++) {
								html.push('<td style="width:33.3%;"></td>');
							}
						}
						if ((count % column) == (column - 1)) {
							html.push('</tr>');
						}
						count++;
					}

				}
				return html.join('');
			};
			/*------------------------------数组类型------------------------------*/
			print.paintArray = function(data, propertyKey) {
				var html = [];
				var pName = thisUtilities.ResultItem[propertyKey].pName;
				var cCount = thisUtilities.ResultItem[propertyKey].cCount;
				var count = 0;
				var j = 0;
				var subNames = [];
				if (cCount > 5) {
					while (data[j]) {
						if (j > 0) {
							html.push('<tr><td style="width:100%;" colspan="3"></td></tr>');
						}
						html.push(print.paintObject(data[j], propertyKey, 3));
						j++;
					}
				} else {
					html.push('<tr>');
					for (var key in pName) {
						html.push('<th>' + pName[key] + '</th>');
						count++;
					}
					html.push('</tr>');
					var styleString = "width:" + (100 / count).toFixed(2) + "%;";
					for (var i = 0; i < data.length; i++) {
						html.push('<tr>');
						var k = 0;
						for (var key in pName) {
							subNames = key.split("_");
							if (subNames.length > 2) {
								var j = 0;
								var combinStr = "";
								while (subNames[j]) {
									combinStr += ((j == 0) ? "" : ",") + data[i][subNames[j]];
									j++;
								}
								html.push('<td ' + (0 == k ? 'class="titlefont boldfont"' : 'class="litefont"') + ' style=' + styleString + '>' + combinStr + '</td>');
							} else {
								html.push('<td ' + (0 == k ? 'class="titlefont boldfont"' : 'class="litefont"') + ' style=' + styleString + '>' + data[i][key] + '</td>');
							}
							k++;
						}
						html.push('</tr>');
					}
				}
				return html.join('');
			};
			/*------------------------------画带有图片的对象------------------------------*/
			print.piantPhotoObject = function(data, propertyKey) {
				var html = [];
				var pName = thisUtilities.ResultItem[propertyKey].pName;
				var photo = "";
				if ("id_photo" == propertyKey) {
					photo = pName.idCardPhoto;
					if (data.idCardPhoto) {
						html.push('<tr>');
						html.push('<td style="width:200px;vertical-align:middle;" rowspan="2">');
						html.push('<img id="idCardPhoto"  src="data:image/png;base64,' + data.idCardPhoto + '"/>');
						html.push('</td>');
					}
					html.push('<td style="vertical-align:middle;padding-left:1%;">');
					html.push('<table border="1" cellspacing="0px" class="date-table">');
					html.push('<tr>');
					html.push('<td class="imgtd"><label>' + pName.cid + "：</label><span>" + data.cid + '</span></td>');
					html.push('<td class="imgtd"><label>' + pName.name + "：</label><span>" + data.name + '<span/></td>');
					html.push('</tr>');
					html.push('<tr>');
					html.push('<td class="imgtd"><label>' + pName.message + "：</label><span>" + data.message + '</span></td>');
					html.push('<td class="imgtd">附：个人身份证照片</td>');
					html.push('</tr>');
					html.push('</table>');
					html.push('</td>');
					
					html.push("</tr>");
				} else if ("education" == propertyKey) {
					html.push('<tr>');
					if (data.photo) {
						html.push('<td style="width:200px;vertical-align:middle;">');
						html.push('<img id="photo"  src="data:image/png;base64,' + data.photo + '">');
						html.push('</td>');
					}	
					html.push('<td style="vertical-align:middle;padding-left:1%;">');
					html.push('<table border="1" cellspacing="0px" class="date-table">');
					html.push('<tr>');
					html.push('<td class="imgtd"><label>' + pName.name + "：</label><span>" + data.name + '</span></td>');
					html.push('<td class="imgtd"><label>' + pName.cid + "：</label><span>" + data.cid + '</span></td>');
					html.push("</tr>");
					html.push('<tr>');
					html.push('<td class="imgtd"><label>' + pName.graduate + "：</label><span>" + data.graduate + '</span></td>');
					html.push('<td class="imgtd"><label>' + pName.studyResult + "：</label><span>" + data.studyResult + '</span></td>');
					html.push("</tr>");
					html.push('<tr>');
					html.push('<td class="imgtd"><label>' + pName.educationDegree + "：</label><span>" + data.educationDegree + '</span></td>');
					html.push('<td class="imgtd"><label>' + pName.specialityName + "：</label><span>" + data.specialityName + '</span></td>');
					html.push("</tr>");
					html.push('<tr>');
					html.push('<td class="imgtd"><label>' + pName.enrolDate + "：</label><span>" + data.enrolDate + '</span></td>');
					html.push('<td class="imgtd"><label>' + pName.graduateTime + "：</label><span>" + data.graduateTime + '</span></td>');
					html.push("</tr>");
					html.push('</table>');
					html.push('</td>');
					html.push("</tr>");
					
				}
				return html.join('');
			};
			//免责申明
			print.paintDisclaimer = function() {
				var html = [];
				html.push('<div class="date-box mt30" style="margin-top:35px;">');
				html.push("<div class='disclaimer'>");
				html.push('<div class="rightcontent_innerdiv" style="border-top:1px solid #E0E0E0;">');
				html.push('<div>');
				//html.push('<p>免责说明：</p>');
				html.push('<p>报告阅读须知</p>');
				html.push('<p>1.本报告的著作权属于杭州我要借企业征信有限公司，未经书面许可，不得擅自复制、摘录、编辑、转载、披露和发表。</p>');
				html.push('<p>2.本报告仅供查询机构参考，不起任何证明作用。且本报告仅限查询依照法律法规规章所规定的查询用途使用，不得擅自向任何非查询人或机构泄露和发布本报告的所涉及的任何信息。</p>');
				html.push('<p>3.本报告的生成依据是截至报告时间为止的企业征信数据库中的相关信息。</p>');
				html.push('<p>4.征信机构承诺保持其客观、中立的地位，并保证将这一原则贯穿于信息汇总、加工、整合的全过程中。</p>');
				html.push('<p>5.请妥善保管本报告，并注意保密。</p>');
				html.push('</div>');
				html.push('</div>');
				html.push('</div>');
				html.push('</div>');
				return html.join('');
			};
			print.bindEvent = function() {
				queryreport.selectResultItem();
				$("#main").on("click", "#printreport", function() {
					//					bdhtml = window.document.body.innerHTML;
					//					sprnstr = "<!--startprint-->";
					//					eprnstr = "<!--endprint-->";
					//					prnhtml = bdhtml.substr(bdhtml.indexOf(sprnstr) + 17);
					//					prnhtml = prnhtml.substring(0, prnhtml.indexOf(eprnstr));
					//					window.document.body.innerHTML = prnhtml;
					$(this).hide();
					window.print();
				});
			};

			return print;
		}
	};

	var thisPage = printPage.createNew();
	thisPage.init();

});